export default {
  name: 'employNum',
  data() {
    return {
      form: {
        startdate: '',
        enddate: '',
        city: '',
        pagenum: '1'
      },
      regions: [],
      addressOptions: [],
      addressProps: {
        value: 'code',
        label: 'name',
        children: 'subLevelModelList'
      },
      tableData: [],
      total: 0,
      currentPage: 1,
      loading: false,
      
    }
  },
  created() {
    this.form.startdate = this.$route.params.startdate || '';
    this.form.enddate = this.$route.params.enddate || '';
    this.getTableData();
    this.getCityDate();
  },
  methods: {
    getCityDate() {
      this.$http.get('assets/citys.json').then((res) => {
        // 响应成功回调
        let tempArr = res.body.city;
        tempArr.forEach(el => {
          el.subLevelModelList.forEach(ele => {
            delete ele.subLevelModelList;
          });
        });
        this.addressOptions = tempArr;
      }, (res) => {
        // 响应错误回调
        alert('服务器请求失败');
      });
    },
    changeCity() {
      this.form.city = this.regions[this.regions.length - 1];
    },
    getTableData() {
      this.loading = true
      this.$axios.$post('/app/v1/platform/company/employees', this.form).then(res => {
        if (res.data.message === 'success.') {
          this.tableData = res.data.data.list;
          this.total = parseInt(res.data.data.totalnum);
          this.loading = false;
        }
      })
    },
    handleCurrentChange(val) {
      this.form.pagenum = val + '';
      this.currentPage = val;
      this.getTableData();
    },
    search() {
      if (!this.form.startdate && !this.form.enddate && !this.form.city) {
        this.form.startdate = '';
        this.form.enddate = '';
        this.form.city = '';
      }
      this.form.pagenum = '1';
      this.currentPage = 1;
      this.getTableData();
    },
    importData() {
      this.$axios({
        method: 'post',
        url: '/app/v1/platform/statistic/companyEmployees',
        data: this.form,
        responseType: 'blob'
      }).then(res => {
        this.download(res)
      }).catch((error) => {
        this.$message({
          message: '导出失败',
          type: 'error'
        });
      })
    },
    download(res) {
      if (!res) {
        return
      }
      let bob = new Blob([res.data]);
      let fileName = (res.headers['content-disposition']).split('=')[1];
      fileName = decodeURI(fileName)
      let url = window.URL.createObjectURL(bob)
      let link = document.createElement('a')
      link.style.display = 'none'
      link.href = url
      link.setAttribute('download', fileName)
      document.body.appendChild(link)
      link.click()
    }
  },
  filters: {
    filterTime: function (value) {
      return value.slice(0, 10);
    }
  }
}